package com.leyou.order.controller;

import com.leyou.order.dto.OrderDTO;
import com.leyou.order.dto.OrderVO;
import com.leyou.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 下单
     */
    @PostMapping("/order")
    public ResponseEntity<Long> saveOrder(@RequestBody OrderDTO orderDTO){
        Long orderId = orderService.saveOrder(orderDTO);
        return ResponseEntity.ok(orderId);
    }

    /**
     * 查询订单信息
     */
    @GetMapping("/order/{id}")
    public ResponseEntity<OrderVO> findOrderVoById(@PathVariable Long id){
        OrderVO orderVO = orderService.findOrderVoById(id);
        return ResponseEntity.ok(orderVO);
    }

    /**
     * 获取支付的url地址
     */
    @GetMapping("/order/url/{id}")
    public ResponseEntity<String> getPayUrl(@PathVariable("id") Long orderId){
        String payUrl = orderService.getPayUrl(orderId);
        return ResponseEntity.ok(payUrl);
    }

    /**
     * 查询订单状态
     */
    @GetMapping("/order/state/{id}")
    public ResponseEntity<Integer> findOrderStatus(@PathVariable("id") Long id){
        Integer status = orderService.findOrderStatus(id);
        return ResponseEntity.ok(status);
    }


}
